package com.gr.blog.dept.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gr.blog.dept.dao.DeptDao;
import com.gr.blog.dept.dao.DeptMenuDao;
import com.gr.blog.dept.model.DeptMenuLink;
import com.gr.blog.dept.model.DeptModel;
import com.gr.blog.dept.service.DeptService;
import com.gr.blog.utils.CollectionsUtil;
import com.gr.blog.utils.CommonUtils;
import com.gr.blog.utils.PageHelperUtils;

@Service("deptService")
public class DeptServiceImpl implements DeptService{

	@Autowired
	private DeptDao deptDao;
	@Autowired
	private DeptMenuDao deptMenuDao;
	
	@Override
	public PageHelperUtils<DeptModel> showAllDept(String deptName,int page,int start,int limit) {
		Example example = new Example(DeptModel.class); 
		PageHelper.startPage((page-1)*limit, limit);
		if (CommonUtils.isNotEmpty(deptName)) {
			example.createCriteria().andLike("dgName", "%"+deptName+"%");
		}
		List<DeptModel> deptList = deptDao.selectByExample(example);
		
		PageInfo<DeptModel> pageInfo = new PageInfo<>(deptList);
		if (CollectionsUtil.isListNotEmpty(deptList)) {
			return new PageHelperUtils<>(deptList,(int)pageInfo.getTotal());
		}
		return null;
	}

	@Override
	public int addDeptInfoToDb(Map<String,Object> map) {
		DeptModel dm = new DeptModel(CollectionsUtil.getStringByMap(map, "dgName"),
				CollectionsUtil.getStringByMap(map, "dgType"),
				CollectionsUtil.getStringByMap(map, "operate"),CommonUtils.getDateCurrentTime());
		return deptDao.insert(dm);
	}

	@Override
	public List<DeptModel> selectMenuDeptByDgId(String dgId) {
		return deptDao.selectMenuDeptByDgId(dgId);
	}

	@Override
	public int deleteDeptAndGroupInfo(String dgId) {
		return deptDao.deleteByPrimaryKey(dgId);
	}

	@Override
	public int removeMenuAndDeptByID(Map<String, Object> map) {
		DeptMenuLink dml = new DeptMenuLink(
				Integer.valueOf(CollectionsUtil.getStringByMap(map, "dgid")),
				CollectionsUtil.getStringByMap(map, "menuid"));
		return deptMenuDao.delete(dml);
	}

	@Override
	public int insertMenuDeptToDbById(Map<String, Object> map) {
		DeptMenuLink dml = new DeptMenuLink(
									Integer.valueOf(CollectionsUtil.getStringByMap(map, "dgid")),
									CollectionsUtil.getStringByMap(map, "menuid"),
									CollectionsUtil.getStringByMap(map, "operate"),
									CommonUtils.getDateCurrentTime());
		return deptMenuDao.insert(dml);
	}

	@Override
	public int findMenuAndDeptLinkById(Map<String, Object> map) {
		return deptDao.selectMenuAndDeptLinkById(map);
	}

	@Override
	public List<DeptModel> getDeptInfoByUserName(String uid) {
		return deptDao.selectDeptInfoByUserName(uid);
	}


}
